from django.db import models
from model_utils.models import SoftDeletableModel


class User(SoftDeletableModel):
    """
    用户表
    """
    username = models.CharField(verbose_name='用户名', max_length=255, unique=True)
    nickname = models.CharField(verbose_name='昵称', max_length=255, default='用户名未设置')
    password = models.CharField(verbose_name='密码', max_length=32)
    role_choice = (
        (1, '管理员'),
        (0, '普通用户'),
    )
    role = models.SmallIntegerField(verbose_name='权限', choices=role_choice, default=1)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    def __str__(self):
        return f'username：{self.username}，nickname：{self.nickname}'

    class Meta:
        db_table = 'cv_user'

    # 用户信息转换
    def role_int2role_message(self):
        """
        将数字权限信息转换为字符权限信息
        """
        if self.username == 'root':
            self.role = '超级管理员'
        elif self.role == 0:
            self.role = '普通用户'
        elif self.role == 1:
            self.role = '管理员'


class Config(SoftDeletableModel):
    """
    配置中心
    """
    config_key = models.CharField(verbose_name='配置名称', max_length=255, unique=True)
    config_value = models.CharField(verbose_name='配置值', max_length=255)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now=True)

    def __str__(self):
        return f'配置名称:{self.config_key}，配置值:{self.config_value}'

    class Meta:
        db_table = 'cv_config'
